#include <sys_config.h>
#include <types.h>
#include <basic_types.h>
#include <mediatypes.h>
#include <api/libc/printf.h>
#include <api/libc/string.h>
#include <osal/osal.h>
#ifndef NEW_DEMO_FRAME
#include <api/libpub29/lib_pub27.h>
#include <api/libpub29/lib_as.h>
#endif
#include <api/libchunk/chunk.h>
#include <hld/pan/pan_dev.h>
#include <hld/pan/pan.h>
#include <bus/sci/sci.h>
#include <hld/decv/decv.h>

#include <api/libosd/osd_lib.h>

#include "osdobjs_def.h"
#include "string.id"
#include "images.id"
#include "osd_config.h"

#include "control.h"
#include "key.h"

#include "menus_root.h"
#include "win_com.h"
#include "win_com_popup.h"
#include "win_com_list.h"
#include "win_signalstatus.h"

#include "aov_version.h"

#define DEB_PRINTF soc_printf

#define	F_NONE	0
#define F_WARN	1
#define F_DONE 	2

/*******************************************************************************
*	Objects definition
*******************************************************************************/
extern CONTAINER g_win_factory_test;

extern TEXT_FIELD facttest_title;

extern CONTAINER facttest_item0;
extern CONTAINER facttest_item1;
extern CONTAINER facttest_item2;
extern CONTAINER facttest_item3;
extern CONTAINER facttest_item4;
extern CONTAINER facttest_item5;
extern CONTAINER facttest_item6;
extern CONTAINER facttest_item7;
extern CONTAINER facttest_item8;
extern CONTAINER facttest_item9;
extern CONTAINER facttest_item10;
extern CONTAINER facttest_item11;
extern CONTAINER facttest_item12;
//extern CONTAINER facttest_item13;
//extern CONTAINER facttest_item14;
//extern CONTAINER facttest_item15;

extern TEXT_FIELD facttest_txt0;
extern TEXT_FIELD facttest_txt1;
extern TEXT_FIELD facttest_txt2;
extern TEXT_FIELD facttest_txt3;
extern TEXT_FIELD facttest_txt4;
extern TEXT_FIELD facttest_txt5;
extern TEXT_FIELD facttest_txt6;
extern TEXT_FIELD facttest_txt7;
extern TEXT_FIELD facttest_txt8;
extern TEXT_FIELD facttest_txt9;
extern TEXT_FIELD facttest_txt10;
extern TEXT_FIELD facttest_txt11;
extern TEXT_FIELD facttest_txt12;
//extern TEXT_FIELD facttest_txt13;
//extern TEXT_FIELD facttest_txt14;
//extern TEXT_FIELD facttest_txt15;

extern BITMAP     facttest_bmp0;
extern BITMAP     facttest_bmp1;
extern BITMAP     facttest_bmp2;
extern BITMAP     facttest_bmp3;
extern BITMAP     facttest_bmp4;
extern BITMAP     facttest_bmp5;
extern BITMAP     facttest_bmp6;
extern BITMAP     facttest_bmp7;
extern BITMAP     facttest_bmp8;
extern BITMAP     facttest_bmp9;
extern BITMAP     facttest_bmp10;
extern BITMAP     facttest_bmp11;
extern BITMAP     facttest_bmp12;
//extern BITMAP     facttest_bmp13;
//extern BITMAP     facttest_bmp14;
//extern BITMAP     facttest_bmp15;


static VACTION  factest_item_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  factest_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);

static VACTION  factest_keymap(POBJECT_HEAD pObj, UINT32 key);
static PRESULT  factest_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2);


#ifdef RECORD_SUPPORT//DVR_PVR_SUPPORT
#define WIN_SH_IDX	  	WSTL_POP_WIN_01_HD//WSTL_TEXT_13 //mailen test
#endif

#define TITLE_SH_IDX	WSTL_POP_TXT_SH_HD

#define CON_SH_IDX   	WSTL_POP_TXT_SH_01_HD
#define CON_HL_IDX   	WSTL_BUTTON_05_HD//WSTL_BUTTON_SELECT_HD
#define CON_SL_IDX   	WSTL_BUTTON_01_HD
#define CON_GRY_IDX  	WSTL_BUTTON_07_HD

#define TXT_SH_IDX   	WSTL_BUTTON_01_FG_HD
#define TXT_HL_IDX   	WSTL_BUTTON_02_FG_HD
#define TXT_SL_IDX   	WSTL_BUTTON_01_FG_HD
#define TXT_GRY_IDX  	WSTL_BUTTON_07_HD

#define BMP_SH_IDX 		WSTL_MIX_BMP_HD
#define BMP_HL_IDX   	WSTL_MIX_BMP_HD
#define BMP_SL_IDX   	WSTL_BUTTON_01_FG_HD
#define BMP_GRY_IDX  	WSTL_BUTTON_01_FG_HD

#define	W_L     120
#define	W_T     0
#define	W_W     (OSD_MAX_WIDTH-2*W_L)
#define	W_H     (OSD_MAX_HEIGHT-2*W_T)
#define W_EDGE	9

#define TITLE_L		W_L
#define TITLE_T		W_T
#define TITLE_W		W_W
#define TITLE_H		38

#define ITEM_L		(W_L+W_EDGE)
#define ITEM_T		(TITLE_T+TITLE_H+ITEM_GAP)
#define ITEM_W		(W_W-W_EDGE*2)
#define ITEM_H		36
#define ITEM_GAP	5

#define ITEM_TXT_L  	10 //offset
#define ITEM_TXT_W  	(ITEM_W-ITEM_BMP_W-2*ITEM_TXT_L-ITEM_TXT_L)//400
#define ITEM_BMP_L  	(ITEM_TXT_L+ITEM_TXT_W+ITEM_TXT_L)
#define ITEM_BMP_W  	35

#define LDEF_TITLE(root,varTxt,nxtObj,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,&root,nxtObj,C_ATTR_ACTIVE,0, \
    0,0,0,0,0, l,t,w,h, TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,TITLE_SH_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_CON(root, varCon,nxtObj,ID,IDu,IDd,l,t,w,h,conobj,focusID)		\
    DEF_CONTAINER(varCon,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,IDu,IDd, l,t,w,h, CON_SH_IDX,CON_HL_IDX,CON_SL_IDX,CON_GRY_IDX,   \
    factest_item_keymap,factest_item_callback,  \
    conobj, 1,1)

#define LDEF_TXT(root,varTxt,nxtObj,ID,l,t,w,h,resID,str)		\
    DEF_TEXTFIELD(varTxt,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, TXT_SH_IDX,TXT_HL_IDX,TXT_SL_IDX,TXT_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_LEFT | C_ALIGN_VCENTER, 0,0,resID,str)

#define LDEF_BMP(root,varBmp,nxtObj,ID,l,t,w,h,icon)	\
  DEF_BITMAP(varBmp,root,nxtObj,C_ATTR_ACTIVE,0, \
    ID,ID,ID,ID,ID, l,t,w,h, BMP_SH_IDX,BMP_HL_IDX,BMP_SL_IDX,BMP_GRY_IDX,   \
    NULL,NULL,  \
    C_ALIGN_CENTER | C_ALIGN_VCENTER, 0,0,icon)

#define LDEF_ITEM(root,varCon,nxtObj,varTxt,varBmp,ID,IDu,IDd,l,t,w,h,str)	\
	LDEF_CON(&root,varCon,nxtObj,ID,IDu,IDd,l,t,w,h,&varTxt,1)	\
	LDEF_TXT(&varCon,varTxt,&varBmp,1,l + ITEM_TXT_L, t,ITEM_TXT_W,h,0,str)	\
	LDEF_BMP(&varCon,varBmp,NULL, 0,l + ITEM_BMP_L, t,ITEM_BMP_W, h,0)


#define LDEF_WIN(varWnd,nxtObj,l,t,w,h,focusID)		\
    DEF_CONTAINER(varWnd,NULL,nxtObj,C_ATTR_ACTIVE,0, \
	    0,0,0,0,0, l,t,w,h, WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,WIN_SH_IDX,   \
	    factest_keymap,factest_callback,  \
	    nxtObj, focusID,0)

LDEF_TITLE(g_win_factory_test, facttest_title,&facttest_item0, \
	TITLE_L, TITLE_T, TITLE_W, TITLE_H,0,len_display_str)


LDEF_ITEM(g_win_factory_test,facttest_item0,&facttest_item1,facttest_txt0,facttest_bmp0,1,13,2, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*0, ITEM_W, ITEM_H,display_strs[0])

LDEF_ITEM(g_win_factory_test,facttest_item1,&facttest_item2,facttest_txt1,facttest_bmp1,2,1,3, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*1, ITEM_W, ITEM_H,display_strs[1])

LDEF_ITEM(g_win_factory_test,facttest_item2,&facttest_item3,facttest_txt2,facttest_bmp2,3,2,4, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*2, ITEM_W, ITEM_H,display_strs[2])

LDEF_ITEM(g_win_factory_test,facttest_item3,&facttest_item4,facttest_txt3,facttest_bmp3,4,3,5, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*3, ITEM_W, ITEM_H,display_strs[3])

LDEF_ITEM(g_win_factory_test,facttest_item4,&facttest_item5,facttest_txt4,facttest_bmp4,5,4,6, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*4, ITEM_W, ITEM_H,display_strs[4])

LDEF_ITEM(g_win_factory_test,facttest_item5,&facttest_item6,facttest_txt5,facttest_bmp5,6,5,7, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*5, ITEM_W, ITEM_H,display_strs[5])

LDEF_ITEM(g_win_factory_test,facttest_item6,&facttest_item7,facttest_txt6,facttest_bmp6,7,6,8, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*6, ITEM_W, ITEM_H,display_strs[6])

LDEF_ITEM(g_win_factory_test,facttest_item7,&facttest_item8,facttest_txt7,facttest_bmp7,8,7,9, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*7, ITEM_W, ITEM_H,display_strs[7])

LDEF_ITEM(g_win_factory_test,facttest_item8,&facttest_item9,facttest_txt8,facttest_bmp8,9,8,10, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*8, ITEM_W, ITEM_H,display_strs[8])

LDEF_ITEM(g_win_factory_test,facttest_item9,&facttest_item10,facttest_txt9,facttest_bmp9,10,9,11, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*9, ITEM_W, ITEM_H,display_strs[9])

LDEF_ITEM(g_win_factory_test,facttest_item10,&facttest_item11,facttest_txt10,facttest_bmp10,11,10,12, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*10, ITEM_W, ITEM_H,display_strs[10])

LDEF_ITEM(g_win_factory_test,facttest_item11,&facttest_item12,facttest_txt11,facttest_bmp11,12,11,13, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*11, ITEM_W, ITEM_H,display_strs[11])

LDEF_ITEM(g_win_factory_test,facttest_item12,NULL,facttest_txt12,facttest_bmp12,13,12,1, \
	ITEM_L, ITEM_T + (ITEM_H + ITEM_GAP)*12, ITEM_W, ITEM_H,display_strs[12])

LDEF_WIN(g_win_factory_test, &facttest_title,W_L, W_T, W_W, W_H,1)

/*******************************************************************************
*	Local vriable & function declare
*******************************************************************************/
char* facttest_item_strs[] = 
{

	"01. 7-Segment Test 1",
	"02. Button Test 1",
	"03. Lock 610/8Mhz",
	"04. Lock 474/8Mhz",
	"05. Audio Test",
	"06. Lock 858/8Mhz",
	"07. USB Test",
	"08. Burn-in Test Toggle",
	"09. Auto Power Off Test",
	"10. 7-Segment Test 2",
	"11. Button Test 2",
	"12. Exit/Set to Default",
	"13. HDCP Key Install",
};

enum ITEM_ID
{
	LED1_ID=1,
	BTN1_ID,
	LOCK1_ID,
	LOCK2_ID,
	AUD_ID,
	LOCK3_ID,
	USB_ID,
	BURN_ID,
	APWR_ID,
	LED2_ID,
	BTN2_ID,
	DEFAULT_ID,
	HDCP_ID,
};

CONTAINER* factest_items[] =
{
	&facttest_item0,
	&facttest_item1,
	&facttest_item2,
	&facttest_item3,
	&facttest_item4,
	&facttest_item5,
	&facttest_item6,
	&facttest_item7,
	&facttest_item8,	
	&facttest_item9,	
	&facttest_item10,
	&facttest_item11,
	&facttest_item12,
};


TEXT_FIELD* factest_item_txts[] =
{
	&facttest_txt0,
	&facttest_txt1,
	&facttest_txt2,
	&facttest_txt3,
	&facttest_txt4,
	&facttest_txt5,
	&facttest_txt6,
	&facttest_txt7,
	&facttest_txt8,	
	&facttest_txt9,	
	&facttest_txt10,
	&facttest_txt11,
	&facttest_txt12,
};

BITMAP* factest_item_bmps[] =
{
	&facttest_bmp0,
	&facttest_bmp1,
	&facttest_bmp2,
	&facttest_bmp3,
	&facttest_bmp4,
	&facttest_bmp5,
	&facttest_bmp6,
	&facttest_bmp7,
	&facttest_bmp8,
	&facttest_bmp9,
	&facttest_bmp10,
	&facttest_bmp11,
	&facttest_bmp12,
};
#if (AOV_HDCP_KEY_ENABLE == SYS_FUNC_ON)
#define FACTROY_TEST_ITEM_CNT ARRAY_SIZE(factest_item_txts)
#else
#define FACTROY_TEST_ITEM_CNT ARRAY_SIZE(factest_item_txts)-1
#endif

static void  factest_set_initdisplay(void);
static void  factest_set_bmpdisplay(void);
static void  factest_draw_bmp(UINT32 bid);

static BOOL b_burnin_on=FALSE;
static UINT8 lock_idx;

typedef INT32 (*factory_test_func)(UINT32 idx, UINT32 bauto);

void factory_set_result(UINT8 id, UINT32 flag)
{
	SYSTEM_DATA *p_data=sys_data_get();
	p_data->factory_item[id-1]=flag;
}

UINT32 factory_get_result(UINT8 id)
{
	SYSTEM_DATA *p_data=sys_data_get();
	return p_data->factory_item[id-1];
}

INT32 factory_test_Null(void)
{
	libc_printf("NULL function\n");
	return 0;
}

INT32 factory_test_lock610(UINT32 idx, UINT32 bauto)
{
	lock_idx=1;
	key_pan_display( "F610" , 4);
	scanchannel( 610000 , 8000 );
	return 1;
}

INT32 factory_test_lock474(UINT32 idx, UINT32 bauto)
{
	lock_idx=2;
	key_pan_display( "F474" , 4);
	scanchannel( 474000 , 8000 );
	return 1;
}

INT32 factory_test_lock858(UINT32 idx, UINT32 bauto)
{
	lock_idx=3;
	key_pan_display( "F858" , 4);
	scanchannel( 858000 , 8000);
	return 1;
}

INT set_factory_scan_result(BOOL flag)
{
	if (lock_idx==1)
		factory_set_result(LOCK1_ID,flag?F_DONE:F_WARN);
	else if (lock_idx==2)
		factory_set_result(LOCK2_ID,flag?F_DONE:F_WARN);
	else if (lock_idx==3)
		factory_set_result(LOCK3_ID,flag?F_DONE:F_WARN);

	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&g_win_factory_test, FALSE);
	return 0;
}

INT factory_test_led(UINT32 idx, UINT32 bauto)
{
	int i;
	char led[10];
	UINT8 back_saved;
	UINT8 ret;
	win_compopup_init(WIN_POPUP_TYPE_SMSG);                        
	win_compopup_set_msg_ext("Check 7-Segment LED",NULL,0);
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(1000);
	win_compopup_smsg_restoreback();
	
	for (i=0; i<4; i++)
	{
		if (i%2)
		{
			key_pan_display("88:88",5);
		}
		else
		{
			key_pan_display("    ",4);
		}
		osal_task_sleep(1000);
	}

	win_compopup_init(WIN_POPUP_TYPE_OKNO);
	win_compopup_set_msg_ext("7-Segment LCD Test OK?",NULL,0);
	win_compopup_set_default_choice(WIN_POP_CHOICE_NO);
	ret = win_compopup_open_ext(&back_saved);
	if(ret==WIN_POP_CHOICE_YES)
	{	
		factory_set_result(idx,F_DONE);
	}
	else
	{	
		factory_set_result(idx,F_WARN);
	}
	win_compopup_smsg_restoreback();
	return 0;
}

INT factory_test_default(UINT32 idx, UINT32 bauto)
{
	Aov_Factory_Load_Default();
	return 0;
}

INT factory_test_auto_poweroff(UINT32 idx, UINT32 bauto)
{
	factory_set_result(APWR_ID,F_DONE);
	Auto_Power_Off_Test();
	return 0;
}

INT factory_test_burnin(UINT32 idx, UINT32 bauto)
{
	UINT8 back_saved;
	SYSTEM_DATA *p_data=sys_data_get();

	recreate_prog_view(VIEW_ALL | PROG_TV_MODE , 0);
	UINT16 prog_num = get_prog_num(VIEW_ALL|PROG_TV_MODE, 0);

	if (prog_num==0)
	{
		key_pan_display("noCH",4);
		win_compopup_init(WIN_POPUP_TYPE_SMSG);                        
		//win_compopup_set_frame(250, 200, 450,200);
		win_compopup_set_msg_ext("No program. Lock channel first",NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		key_pan_display("tESt",4);
		return 1;
	}

	if (b_burnin_on==TRUE)
	{
		p_data->burn_in = b_burnin_on = FALSE;
	    Stop_Factory_Lock_Test();
		win_compopup_init(WIN_POPUP_TYPE_SMSG);                        
		win_compopup_set_msg_ext("Lock Test OFF",NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		factory_set_result(BURN_ID,F_DONE);
		key_pan_display("tESt",4);
	}
	else
	{
		win_compopup_init(WIN_POPUP_TYPE_SMSG);
		win_compopup_set_msg_ext("Lock Test ON",NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		p_data->burn_in = b_burnin_on = TRUE;
	    Start_Factory_Lock_Test();
	}
	sys_data_save(1);
	return 0;	
}

INT factory_test_burnin_start(void)
{
	UINT8 back_saved;

	b_burnin_on = TRUE;
    Start_Factory_Lock_Test();
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_msg_ext("Lock Test ON",NULL,0);
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(1500);
	win_compopup_smsg_restoreback();
	return 0;
}

INT factory_test_hdcp(UINT32 idx, UINT32 bauto)
{
	OSD_ClearObject(&g_win_factory_test, C_UPDATE_ALL);
	osal_task_sleep(200);
   	AOV_Install_HDCP();
   	OSD_TrackObject(&g_win_factory_test, C_DRAW_SIGN_EVN_FLG | C_UPDATE_ALL);
	return 0;
}

static BOOL key_dn_ok=FALSE, key_up_ok=FALSE, key_pwr_ok=FALSE;
static UINT8 check_seq_no=0;
INT factory_test_button(UINT32 idx, UINT32 bauto)
{
	UINT8 back_saved;
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_msg_ext("Press Keypad Down->Up->Power",NULL,0);
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(1500);
	win_compopup_smsg_restoreback();

	key_up_ok=key_dn_ok=key_pwr_ok=FALSE;
	check_seq_no=0;
	
	return 0;
}

INT set_factory_USB_result(BOOL flag)
{
	CHENGYU_PRINTF("set_factory_USB_result ==> flag(%d)\n", flag);
	if (flag)
	{
		factory_set_result(USB_ID,F_DONE);
		factest_set_bmpdisplay();
		ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&g_win_factory_test, FALSE);
	}
	return 0;
}

INT factory_test_USB(UINT32 idx, UINT32 bauto)
{
	UINT8 back_saved;
	//HAL_GPIO_BIT_SET(AOV_GPIO_USB_PWR_CTRL, 0); //1: USB power-on mode, 0:USB standby mode
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_msg_ext("Plug in USB Disk",NULL,0);
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(1500);
	win_compopup_smsg_restoreback();
	//HAL_GPIO_BIT_SET(AOV_GPIO_USB_PWR_CTRL, 1); //1: USB power-on mode, 0:USB standby mode

	return 0;
}


INT factory_test_audio(UINT32 idx, UINT32 bauto)
{
	UINT8 back_saved;
	UINT8 ret;
	UINT16 prog_num;
	SYSTEM_DATA *p_data=sys_data_get();
	struct snd_device *snd = dev_get_by_id(HLD_DEV_TYPE_SND, 0);
		
	recreate_prog_view(VIEW_ALL | PROG_TV_MODE , 0);
	prog_num = get_prog_num(VIEW_ALL|PROG_TV_MODE, 0);
			
	if (prog_num==0)
	{
		key_pan_display("noCH",4);
		win_compopup_init(WIN_POPUP_TYPE_SMSG);                        
		win_compopup_set_msg_ext("No program. Lock channel first",NULL,0);
		win_compopup_open_ext(&back_saved);
		osal_task_sleep(1000);
		win_compopup_smsg_restoreback();
		key_pan_display("tESt",4);
		return 0;
	}

	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	//win_compopup_set_frame(50, 200, 250,200);
	win_compopup_set_msg_ext("Left Audio",NULL,0);

	chchg_switch_audio_chan(snd, 0);//Left audio channel
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(3000);
	win_compopup_smsg_restoreback();

	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	//win_compopup_set_frame(650, 200, 250,200);
	win_compopup_set_msg_ext("Right Audio",NULL,0);
	chchg_switch_audio_chan(snd, 1);//right audio channel
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(3000);
	win_compopup_smsg_restoreback();

	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	//win_compopup_set_frame(350, 200, 250,200);
	win_compopup_set_msg_ext("Stereo Audio",NULL,0);
	chchg_switch_audio_chan(snd, 2);//stereo audio channel
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(3000);
	win_compopup_smsg_restoreback();

	win_compopup_init(WIN_POPUP_TYPE_OKNO);
	win_compopup_set_msg_ext("Audio Test OK?",NULL,0);
	win_compopup_set_default_choice(WIN_POP_CHOICE_NO);
	ret = win_compopup_open_ext(&back_saved);
	win_compopup_smsg_restoreback();

	if(ret==WIN_POP_CHOICE_YES)
	{
		factory_set_result(AUD_ID,F_DONE);
	}
	else
	{
		factory_set_result(AUD_ID,F_WARN);
	}

	ap_send_msg(CTRL_MSG_SUBTYPE_CMD_ENTER_ROOT, (UINT32)&g_win_factory_test, FALSE);

	return 0;
}

factory_test_func factory_test_funcs[] = 
{
	factory_test_led,			// 1
	factory_test_button,		// 2
	factory_test_lock610,		// 3
	factory_test_lock474,		// 4
	factory_test_audio,			// 5
	factory_test_lock858,		//6
	factory_test_USB,			//7
	factory_test_burnin,		//8
	factory_test_auto_poweroff, //9
	factory_test_led,			//10
	factory_test_button,		//11
	factory_test_default,		//12	
	factory_test_hdcp,			//13
};


/*******************************************************************************
*	key mapping and event callback definition
*******************************************************************************/
#define VACT_CH_UP	(VACT_PASS+1)
#define VACT_CH_DN	(VACT_PASS+2)
#define VACT_POWER	(VACT_PASS+3)

static VACTION  factest_item_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;

	switch(key)
	{
	case V_KEY_ENTER:
		act = VACT_ENTER;
		break;

		case V_KEY_C_UP:
			act=VACT_CH_UP;
			break;

		case V_KEY_C_DOWN:
			act=VACT_CH_DN;
			break;

//Gavim@factory------
		case V_KEY_UP:
			act=VACT_CH_UP;
			break;

		case V_KEY_DOWN:
			act=VACT_CH_DN;
			break;
         //-------			
		case V_KEY_POWER:
			act=VACT_POWER;
			break;
			
	default:
		act = VACT_PASS;
	}

	return act;
}

void show_popup_pankey_checked(VACTION act)
{
	UINT8 back_saved;
	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	//win_compopup_set_frame(250, 200, 450,200);
	switch (act)
	{
		case VACT_CH_DN:
			win_compopup_set_msg_ext("DOWN",NULL,0);
			if (check_seq_no==0)
				key_dn_ok=TRUE;
			break;

		case VACT_CH_UP:
			win_compopup_set_msg_ext("UP",NULL,0);
			if (check_seq_no==1)
				key_up_ok=TRUE;
			break;

		case VACT_POWER:
			win_compopup_set_msg_ext("POWER",NULL,0);
			if (check_seq_no==2)
				key_pwr_ok=TRUE;
			break;

		default:
			win_compopup_set_msg_ext("XXX",NULL,0);
			break;
	}
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(400);
	win_compopup_smsg_restoreback();
}

void show_popup_messge(char* str)
{
	UINT8 back_saved;

	win_compopup_init(WIN_POPUP_TYPE_SMSG);
	win_compopup_set_msg_ext(str, NULL,0);
	win_compopup_open_ext(&back_saved);
	osal_task_sleep(1500);
	win_compopup_smsg_restoreback();
}

//ChengYu@20140515, factory mode improvement
BOOL check_item_valid(UINT8 id)
{
	BOOL ret=FALSE;

	if((factory_get_result(LED1_ID)!=F_DONE) && (id>LED1_ID))
		show_popup_messge("Warning: Test Item 01 not pass yet");
	else if((factory_get_result(BTN1_ID)!=F_DONE) && (id>BTN1_ID))
		show_popup_messge("Warning: Test Item 02 not pass yet");
	else if((factory_get_result(LOCK1_ID)!=F_DONE) && (id>LOCK1_ID))
		show_popup_messge("Warning: Test Item 03 not pass yet");
	else if((factory_get_result(LOCK2_ID)!=F_DONE) && (id>LOCK2_ID))
		show_popup_messge("Warning: Test Item 04 not pass yet");
	else if((factory_get_result(AUD_ID)!=F_DONE) && (id>AUD_ID))
		show_popup_messge("Warning: Test Item 05 not pass yet");
	else if((factory_get_result(LOCK3_ID)!=F_DONE) && (id>LOCK3_ID))
		show_popup_messge("Warning: Test Item 06 not pass yet");
	else if((factory_get_result(USB_ID)!=F_DONE) && (id>USB_ID))
		show_popup_messge("Warning: Test Item 07 not pass yet");
	else if((!b_burnin_on) && (id==APWR_ID))
		show_popup_messge("Warning: Burn-in mode not open yet");
	else if((factory_get_result(BURN_ID)!=F_DONE) && (id>APWR_ID))
		show_popup_messge("Warning: Test Item 08 not pass yet");
	else if((factory_get_result(APWR_ID)!=F_DONE) && (id>APWR_ID))
		show_popup_messge("Warning: Test Item 09 not pass yet");
	else if((factory_get_result(LED2_ID)!=F_DONE) && (id>LED2_ID))
		show_popup_messge("Warning: Test Item 10 not pass yet");
	else if((factory_get_result(BTN2_ID)!=F_DONE) && (id>BTN2_ID))
		show_popup_messge("Warning: Test Item 11 not pass yet");
	else
		ret=TRUE;

	return ret;
}

static PRESULT  factest_item_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	UINT8 bID;
	POBJECT_HEAD nxtObj;
	SYSTEM_DATA *p_data=sys_data_get();
	VACTION unact;
	bID = OSD_GetObjID(pObj);
	UINT32 vkey;

	switch(event)
	{
	case EVN_UNKNOWN_ACTION:		
		{
			unact = (VACTION)(param1>>16);
			//CHENGYU_PRINTF("factest_item_callback ==> EVN_UNKNOWN_ACTION\n");
			if (unact==VACT_ENTER)
			{
				if(b_burnin_on && (bID!=BURN_ID) && (bID!=APWR_ID))//ChengYu@20140515, factory mode improvement
				{
					show_popup_messge("Still in BurnIn mode");
					return ret;
				}
				// Gavim@20150210
				/*
				if(!check_item_valid(bID))//ChengYu@20140515, factory mode improvement
					return ret;
				*/
				p_data->factory_item[bID-1]=F_NONE;
				factory_test_funcs[bID-1](bID, 0); //mailen test
			}
			else if (bID==BTN1_ID || bID==BTN2_ID)
			{		
				if (unact==VACT_CH_DN && check_seq_no==0)
				{
					show_popup_pankey_checked(VACT_CH_DN);
					check_seq_no=1;
				}
				else if (unact==VACT_CH_UP && check_seq_no==1)
				{
					show_popup_pankey_checked(VACT_CH_UP);
					check_seq_no=2;
				}
				else if (unact==VACT_POWER && check_seq_no==2)
				{
					show_popup_pankey_checked(VACT_POWER);
					check_seq_no=0;
				}
				else p_data->factory_item[bID-1]=F_WARN;
				
				if (key_up_ok && key_dn_ok && key_pwr_ok)
				{
					key_dn_ok=key_up_ok=key_pwr_ok=FALSE;
					p_data->factory_item[bID-1]=F_DONE;
				}
			}

			factest_set_bmpdisplay();
			
			nxtObj = (POBJECT_HEAD)&facttest_item0;
			while(nxtObj)
			{
				if(nxtObj != pObj)
					OSD_DrawObject(nxtObj,C_UPDATE_ALL);
				else
					OSD_TrackObject(nxtObj,C_UPDATE_ALL);
				nxtObj = OSD_GetObjpNext(nxtObj);
			}			
		
			//update_data();
			sys_data_save(1);
		}
		break;
	}

	return ret;		
}

static VACTION  factest_keymap(POBJECT_HEAD pObj, UINT32 key)
{
	VACTION act;
	UINT8 back_saved;
	UINT8 bID = OSD_GetObjID(pObj);

	switch(key)
	{
		case V_KEY_ENTER:		
			act = VACT_ENTER;
			break;
  		case V_KEY_UP:
	        	act = VACT_CURSOR_UP;
	        	break;        	
    		case V_KEY_DOWN:
	        	act = VACT_CURSOR_DOWN;
			break;	
//Gavim@factory-----------------			
		case V_KEY_V_UP:
			libc_printf("V_KEY_V_UP\n");
			act = VACT_CURSOR_UP;
			break;
		case V_KEY_V_DOWN:
			libc_printf("V_KEY_V_DOWN\n");
			act = VACT_CURSOR_DOWN;
			break;
//-----------			
		case V_KEY_EXIT:
		case V_KEY_MENU:
			{
				UINT16 channel = sys_data_get_cur_group_cur_mode_channel();
				P_NODE p_node;
				UINT16 prog_num_tv, prog_num_radio;
			
				recreate_prog_view(VIEW_ALL | PROG_TV_MODE , 0);
				prog_num_tv = get_prog_num(VIEW_ALL|PROG_TV_MODE, 0);
			
				recreate_prog_view(VIEW_ALL | PROG_RADIO_MODE , 0);
				prog_num_radio = get_prog_num(VIEW_ALL|PROG_RADIO_MODE, 0);
			
				if ( (prog_num_tv+prog_num_radio)!=0 )
				{
					if (prog_num_tv==0)
					{
						sys_data_set_cur_chan_mode(PROG_RADIO_MODE);
						recreate_prog_view(VIEW_ALL | PROG_RADIO_MODE , 0);
					}
					else
					{
						sys_data_set_cur_chan_mode(PROG_TV_MODE);
						recreate_prog_view(VIEW_ALL | PROG_TV_MODE , 0);
					}
					act = VACT_CLOSE;
				}
				else
				{
					win_compopup_init(WIN_POPUP_TYPE_SMSG);
					win_compopup_set_msg_ext("No program. Lock channel first", NULL,0);
					win_compopup_open_ext(&back_saved);
					osal_task_sleep(2000);
					win_compopup_smsg_restoreback();
					act = VACT_PASS;
				}
			}
			break;
		default:
			act = VACT_PASS;	
	}

	return act;
}

static PRESULT  factest_callback(POBJECT_HEAD pObj, VEVENT event, UINT32 param1, UINT32 param2)
{
	PRESULT ret = PROC_PASS;
	POBJECT_HEAD topmenu;
#if 1//ChengYu@20140512, change focus on the undone item
	UINT32 i;
	UINT8 bNewFocusID=1;
#endif

	switch(event)
	{
	case EVN_PRE_OPEN:
		sys_data_select_language(0);//ChengYu@20140512, change OSD language to English in factory mode 
		epg_off();			
		si_monitor_off(0xFFFFFFFF);
		api_osd_mode_change(OSD_NO_SHOW);

		key_pan_display("tESt",4);
		wincom_close_title();

		menu_stack_pop_all();
		ap_clear_all_menus();
		
		OSD_ClearObject( (POBJECT_HEAD) &g_win_mainmenu, C_UPDATE_ALL);
		OSD_ClearObject( (POBJECT_HEAD) &g_win_welcom, C_UPDATE_ALL);
		
		factest_set_initdisplay();

#if (AOV_HDCP_KEY_ENABLE == SYS_FUNC_OFF)
		remove_menu_item(&g_win_factory_test, &facttest_item12, ITEM_H+ITEM_GAP);
#endif
		break;

	case EVN_POST_OPEN:
#if 1//ChengYu@20140512, change focus on the undone item
		for(i=1; i<FACTROY_TEST_ITEM_CNT; i++)
		{
			if(factory_get_result(i)==F_DONE)
				bNewFocusID=i+1;
			else
				break;
		}
		OSD_ContainerChgFocus(&g_win_factory_test, bNewFocusID,C_DRAW_SIGN_EVN_FLG | C_UPDATE_FOCUS);
#endif
		break;

	case EVN_PRE_CLOSE:		
		break;

	case EVN_POST_CLOSE:
		break;

	case EVN_MSG_GOT:
		//CHENGYU_PRINTF("factest_callback ==> EVN_MSG_GOT param1(%u) param2(%u)\n",param1,param2);
		switch(param1)
		{
			case CTRL_MSG_SUBTYPE_CMD_EXIT:
				sys_data_save(1);
				ret = PROC_LEAVE;
				break;

			case CTRL_MSG_SUBTYPE_CMD_STO:
				if(param2 == USB_STATUS_OVER)
				{
					if(factory_get_result(USB_ID)!=F_DONE)
						factory_set_result(USB_ID,F_WARN);
					factest_draw_bmp(USB_ID);
				}
				ret = PROC_PASS;
				break;

			default:
				ret = PROC_PASS;
				break;
		}
		break;
		
	}

	return ret;
}

static void  factest_set_initdisplay(void)
{
	TEXT_FIELD	*txt;
	BITMAP		*bmp;		
	char 		str[50];

	unsigned long chid;
	CHUNK_HEADER blk_header;
	UINT32 i;

	sprintf(str, "%s-%s-%s", AOV_INFO_CUSTOMER_NAME, AOV_SYS_INFO_MODEL_NAME, AOV_SYS_INFO_VERSION);
	ComAscStr2Uni(str, len_display_str);
	
	txt = &facttest_title;
	OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)str);

	for(i=0;i<FACTROY_TEST_ITEM_CNT;i++)
	{
		txt = factest_item_txts[i];
		OSD_SetTextFieldContent(txt, STRING_ANSI, (UINT32)facttest_item_strs[i]);		
	}

	factest_set_bmpdisplay();
	
}

static void  factest_set_bmpdisplay(void)
{
	TEXT_FIELD	*txt;
	BITMAP		*bmp;		
	UINT32 i;

	SYSTEM_DATA *p_data=sys_data_get();

	for(i=0;i<FACTROY_TEST_ITEM_CNT;i++)
	{
		txt = factest_item_txts[i];
		bmp = (BITMAP*)OSD_GetObjpNext(txt);
		if(bmp != NULL)
		{
			if (p_data->factory_item[i]==F_DONE)
				OSD_SetBitmapContent(bmp, IM_MSELECT);
			else if (p_data->factory_item[i]==F_WARN)
				OSD_SetBitmapContent(bmp, IM_TV_DEL);
			else if (p_data->factory_item[i]==F_NONE)
				OSD_SetBitmapContent(bmp, 0);
		}
	}	
}

static void  factest_draw_bmp(UINT32 bid)
{
	TEXT_FIELD	*txt;
	BITMAP		*bmp;		
	UINT32 i;

	SYSTEM_DATA *p_data=sys_data_get();

	if( bid<= 0 || bid>FACTROY_TEST_ITEM_CNT)
		return;

	i = bid-1;
	txt = factest_item_txts[i];
	bmp = (BITMAP*)OSD_GetObjpNext(txt);
	if(bmp != NULL)
	{
		if (p_data->factory_item[i]==F_DONE)
			OSD_SetBitmapContent(bmp, IM_MSELECT);
		else if (p_data->factory_item[i]==F_WARN)
			OSD_SetBitmapContent(bmp, IM_TV_DEL);
		else if (p_data->factory_item[i]==F_NONE)
			OSD_SetBitmapContent(bmp, 0);
	}
	OSD_DrawBitmapCell(bmp,0,C_UPDATE_CONTENT);
}
